# Contributor: Roman Ustyugov <qtdevel@gmail.com>
# Contributor: Joker-jar <joker-jar@yandex.ru>

pkgname="psi+"
pkgver=3835
pkgrel=1
pkgdesc="Psi IM Mod by psi-dev@conference.jabber.ru"
url="http://psi-plus.com"
license=('GPL2')
arch=('i686' 'x86_64')
depends=('qt' 'qca' 'qca-ossl' 'qca-gnupg' 'aspell' 'libxss' 'openssl' 'dbus' 'zlib')
makedepends=('subversion' 'git' 'patch')
conflicts=('psi' 'psi-svn' 'psi-git' 'psi-plus-svn')
provides=('psi+' 'psi-plus-svn' 'psi')
 
# Make quietly (say "0" here if you want to see all output, "1" - only stderr, "2" - nothing)
# values: (0|1|2)
quietmake=0
# Include plugins to package
# values: (0|1)
incplugins=1
# Include skins to package
# values: (0|1)
incskins=0
# Plugins list. Excludes/includes certain plugins (if incplugins=1)
# Available plugins: attentionplugin, autoreplyplugin, birthdayreminderplugin, captchaformsplugin, chessplugin,
#                    cleanerplugin, conferenceloggerplugin, contentdownloaderplugin, extendedoptionsplugin, gmailnotifyplugin,
#                    historykeeperplugin, icqdieplugin, imageplugin, juickplugin, qipxstatusesplugin,
#                    screenshotplugin, skinsplugin, stopspamplugin, storagenotesplugin, translateplugin,
#                    videostatusplugin, watcherplugin
# Example: plugins_list=('imageplugin' 'translateplugin')
#          plugins_list_rule='include'
# includes only imageplugin and translateplugin to package
plugins_list=()
# Say "include" to include only specified in the plugins_list section plugins (if incplugins=1)
# values: (exclude|include)
plugins_list_rule='exclude'
# Use mva's russian langpack
# values: (0|1)
usemvalang=0
# Use WebKit (http://code.google.com/p/psi-dev/wiki/webkit)
# CAUTION: webkit is EXPERIMENTAL feature! Use it at your own risk!
# values: (0|1)
usewebkit=0
# Use powersave
# values: (0|1)
powersave=0
# Use crash handling dialog (only for KDE)
# (Not useful yet, so you may safely say 0 here)
# values: (0|1)
usecrash=0
# Use whiteboarding
# CAUTION: you may see build error if you say "1" here :)
# values: (0|1)
whiteboarding=1
 
_svntrunk="http://psi-dev.googlecode.com/svn/trunk"
_svnmod="patches"
_iconsetssvnmod="iconsets"
_skinssvnmod="skins"
_certssvnmod="certs"
_pluginssvnmod="plugins"
# ----------
_langpacksvntrunk="http://psi-ru.googlecode.com/svn/branches"
_langpacksvnmod="psi-plus"
# ----------
_langpackgitroot="git://mva.name" # mva's langpack
_langpackgitname="psi-l10n-ru"
# ----------
_gitroot__="git://git.psi-im.org/psi.git"
_gitname__="psi"
 
# Options for 'make' utility for parallel compilation
MAKEOPT=${MAKEOPT:--j$((`cat /proc/cpuinfo | grep processor | wc -l`+1))}
 
build() {
if true; then
  cd $srcdir
  # Fix package info (not for aur.archlinux.org)
  #pkgrel=archlinux
  
  revision=$pkgver
  msg2 "SVN revision $revision will be used"

  msg "Getting patches from SVN"
  msg "Connecting to $_svntrunk/$_svnmod ..."
  if [ -d $srcdir/$_svnmod ] ; then
    svn up $_svnmod -r $revision
  else
    svn checkout $_svntrunk/$_svnmod -r $revision
  fi
  msg "SVN Checkout done or server timeout"
  
  msg "Getting iconsets from SVN"
  msg "Connecting to $_svntrunk/$_iconsetssvnmod ..."
  if [ -d $srcdir/$_iconsetssvnmod ] ; then
    svn up $_iconsetssvnmod -r $revision
  else
    svn checkout $_svntrunk/$_iconsetssvnmod -r $revision
  fi
  msg "SVN Checkout done or server timeout"
  
  if [ $incskins -eq "1" ] ; then
    msg "Getting skins from SVN"
    msg "Connecting to $_svntrunk/$_skinssvnmod ..."
    if [ -d $srcdir/$_skinssvnmod ] ; then
      svn up $_skinssvnmod -r $revision
    else
      svn checkout $_svntrunk/$_skinssvnmod -r $revision
    fi
    msg "SVN Checkout done or server timeout"
  fi
  
  msg "Getting certs from SVN"
  msg "Connecting to $_svntrunk/$_certssvnmod ..."
  if [ -d $srcdir/$_certssvnmod ] ; then
    svn up $_svntrunk/$_certssvnmod -r $revision
  else
    svn checkout $_svntrunk/$_certssvnmod -r $revision
  fi
  msg "SVN Checkout done or server timeout"
 
  if [ $incplugins -eq "1" ] ; then
    msg "Getting plugins from SVN"
    msg "Connecting to $_svntrunk/$_pluginssvnmod ..."
    if [ -d $srcdir/$_pluginssvnmod ] ; then
      svn up $_pluginssvnmod -r $revision
    else
      svn checkout $_svntrunk/$_pluginssvnmod -r $revision
    fi
    msg "SVN Checkout done or server timeout"
  fi
 
  if [ $usemvalang -eq "1" ] ; then
    msg "Connecting to $_langpackgitroot/$_langpackgitname ..."
    if [ -d $srcdir/$_langpackgitname ] ; then
      cd $srcdir/$_langpackgitname && git pull origin
      msg "Updating iris ..."
      git submodule update
      msg "The local files are updated."
    else
      git clone $_langpackgitroot/$_langpackgitname
      cd $srcdir/$_langpackgitname
      msg "Getting iris ..."
      git submodule init
      git submodule update
    fi
    msg "GIT checkout done or server timeout"
    cd $srcdir
  else
    msg "Getting langpack from SVN"
    msg "Connecting to $_langpacksvntrunk/$_langpacksvnmod ..."
    if [ -d $srcdir/$_langpacksvnmod ] ; then
      svn up $_langpacksvnmod
    else
      svn checkout $_langpacksvntrunk/$_langpacksvnmod
    fi
    msg "SVN Checkout done or server timeout"
  fi  
 
  msg "Connecting to $_gitroot__ ..."
  if [ -d $srcdir/$_gitname__ ] ; then
    cd $srcdir/$_gitname__ && git pull origin
    msg "Updating iris ..."
    git submodule update
    msg "The local files are updated."
  else
    git clone $_gitroot__
    cd $srcdir/$_gitname__
    msg "Getting iris ..."
    git submodule init
    git submodule update
  fi
  msg "GIT checkout done or server timeout"
 
  [ -d $srcdir/$_gitname__-build ] && rm -r $srcdir/$_gitname__-build
  cp -r $srcdir/$_gitname__ $srcdir/$_gitname__-build
 
  msg "Copying patches to build directory"
  mkdir $srcdir/$_gitname__-build/patches
  cp -r $srcdir/$_svnmod/* $srcdir/$_gitname__-build/patches/
 
  msg "Applying patches"
  cd $srcdir/$_gitname__-build/patches
  #rm -f *-psi-win32-*         #useless windows patches
  #rm -f *dirty-check*         #useless update check
  if [ $powersave -eq "1" ] ; then
    cp dev/psi-reduce-power-consumption.patch psi-reduce-power-consumption.diff
  fi
 
  cd $srcdir/$_gitname__-build
  log=$srcdir/$_gitname__-build/PATCHING.log
  : > $log
  for i in $srcdir/$_gitname__-build/patches/*.diff; do
    pn=$(basename $i)
    msg2 "Applying '$pn'"
    echo "Applying '$pn'" >> $log
    patch -p1 < "$i" >> $log 2>&1
  done
 
  # Get program version
  ver=$(cat src/applicationinfo.cpp | awk 'BEGIN {} { ver = match($0, /PROG_VERSION "..../); if (ver) { print substr($0,RSTART+14,4); exit; }}')
  sed "s/\(.xxx\)/.$revision/" -i src/applicationinfo.cpp
  [ $usewebkit -eq "1" ] && revision=$revision"webkit"
  sleep 0;pkgver="$ver.$revision"
  
  if [ $incplugins -eq "1" ] ; then
    msg "Adding extra plugins"
    cp -rf $srcdir/$_pluginssvnmod/* $srcdir/$_gitname__-build/src/plugins/
  fi
 
  msg "Building $pkgname ..."
  # We'll use system libraries
  rm -rf third-party/qca
  
  # Enable plugins support if disabled
  sed 's/#CONFIG += psi_plugins/CONFIG += psi_plugins/' -i src/src.pro
 
  if [ $usecrash -eq "1" ] ; then
    sed 's/#CONFIG += use_crash/CONFIG += use_crash/' -i src/src.pro
    msg2 "Using crash handler: yes"
  else
    msg2 "Using crash handler: no"
  fi
 
  if [ $powersave -eq "1" ] ; then
    msg2 "Using powersave: yes"
  else
    msg2 "Using powersave: no"
  fi
 
  # Enable whiteboarding
  if [ $whiteboarding -eq "1" ] ; then
    sed 's/#CONFIG += whiteboarding/CONFIG += whiteboarding/' -i src/src.pro
    # Fix undefined qdebug
#    sed '/qDebug/d' -i src/sxe/sxeedit.cpp
#    sed '/qDebug/d' -i src/sxe/sxerecord.cpp
    (cd src/sxe && patch -p1 <$srcdir/whiteboard-build-fix.patch) || true
    msg2 "Using whiteboarding: yes"
    warning "Whiteboarding is very unstable thing. Don't post bug reports about it!"
  else
    msg2 "Using whiteboarding: no"
  fi
 
  # Webkit setup
  if [ $usewebkit -eq "1" ] ; then
    msg2 "Using webkit: yes"
    warning "webkit is VERY COOL feature! Use it at your own risk!"
  else
    msg2 "Using webkit: no"
  fi
 
  msg2 "Fixing iconsets..."
  mkdir -p $srcdir/$_gitname__-build/iconsets/system/default
  cp -r $srcdir/$_iconsetssvnmod/* $srcdir/$_gitname__-build/iconsets/
 
  msg2 "Running configure ..."
  ./configure --prefix=/usr --release --disable-bundled-qca --disable-growl
 
  # Enable webkit support
  [ $usewebkit -eq "1" ] && echo "CONFIG += webkit" >> conf.pri
else
  cd $srcdir/psi-build
fi
  msg2 "Building ..."
  if [ $quietmake -eq "1" ] ; then
    make $MAKEOPT > /dev/null
  else
    if [ $quietmake -eq "2" ] ; then
      make $MAKEOPT > /dev/null 2>&1
    else
      make $MAKEOPT
    fi
  fi
  msg2 "Installing ..."
  make INSTALL_ROOT="$pkgdir" install
 
  # Copy Psi license
  install -D COPYING "$pkgdir/usr/share/licenses/$pkgname/COPYING"

  cd $pkgdir/usr/share/psi
  msg2 "Copying additional files ..."
  cp -rf $srcdir/$_iconsetssvnmod ./
  if [ $incskins -eq "1" ] ; then
    mkdir skins
    cp $srcdir/$_skinssvnmod/default_skin.* ./skins
    cp -rf $srcdir/$_skinssvnmod/universal/* ./skins
  fi
  
  cp -rf $srcdir/$_certssvnmod ./
 
  # Copy langpack
  msg "Copying langpack ..."
  if [ $usemvalang -eq "1" ] ; then
    install -D -m 644 $srcdir/$_langpackgitname/*.qm ./
  else
    install -D -m 644 $srcdir/$_langpacksvnmod/*.qm ./
    install -D -m 644 $srcdir/$_langpacksvnmod/qt/*.qm ./
  fi
 
  if [ $incplugins -eq "1" ] ; then
    msg "Building extra plugins ..."
    cd $srcdir/$_gitname__-build/src/plugins/generic
    # Create project file for extra plugins and compile them
    echo "TEMPLATE = subdirs" > extraplugins.pro
    echo -n "SUBDIRS = " >> extraplugins.pro 
    for pl in ${plugins_list[@]}; do
      if [ -z $matchexp ] ; then
        matchexp=\^$pl\$
      else
        matchexp=$matchexp"|"\^$pl\$
      fi
    done
    matchexp=" ($matchexp)"
    [[ $plugins_list_rule == "exclude" && ${#plugins_list[@]} -ne 0 ]] && matchexp="v$matchexp"
    [[ $plugins_list_rule == "include" && ${#plugins_list[@]} -eq 0 ]] && matchexp="v$matchexp"
    ls -1 $srcdir/$_pluginssvnmod/* | grep -E$matchexp | sed -e :a -e '$b;N;s/\n/ /;ba' >> extraplugins.pro
    qmake extraplugins.pro
    # Fix bad project name
    sed 's/screenshot\.pro/screenshotplugin\.pro/' -i Makefile
    if [ $quietmake -eq "1" ] ; then
      make $MAKEOPT > /dev/null
    else
      if [ $quietmake -eq "2" ] ; then
        make $MAKEOPT > /dev/null 2>&1
        else
      make $MAKEOPT
      fi
    fi
    # Copy plugins
    mkdir $pkgdir/usr/share/psi/plugins
    find . -name "lib*plugin.so" -exec cp -f {} $pkgdir/usr/share/psi/plugins \;
  fi
  
  # Remove useless entries
  find $pkgdir -name ".svn" -type d | xargs rm -rf
  rm -f $pkgdir/usr/bin/*.debug
  rm -rf $srcdir/$_gitname__-build
  # Force locale for russian spellcheck
  #sed 's/Exec=psi/Exec=env LANG=ru_RU\.UTF-8 psi/' -i $pkgdir/usr/share/applications/psi.desktop
}
md5sums=()
md5sums=()
